package team.bluepen.order.data.database.hive;

import space.lingu.light.Database;
import space.lingu.light.Light;
import space.lingu.light.LightDatabase;
import space.lingu.light.log.JdkDefaultLogger;
import team.bluepen.order.data.entity.Order;
import team.bluepen.order.data.database.OrderConnectionPool;
import team.bluepen.order.data.database.mysql.MysqlDialectProvider;
import team.bluepen.order.util.Log;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author RollW
 */
@Database(name = "order_system",
        tables = {Order.class},
        datasourceConfig = "/order.properties", version = 1)
public abstract class OrderHiveDatabase extends LightDatabase {
    private static final String TAG = "OrderHiveDatabase";

    public abstract OrderDao getOrderDao();

    @Override
    public Connection requireConnection() {
        Connection connection = super.requireConnection();
        try {
            PreparedStatement useStmt =
                    connection.prepareStatement(
                            "USE order_system");
            useStmt.execute();
            useStmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    protected void init() {
        try {
            Connection connection = super.requireConnection();
            PreparedStatement dbStmt =
                    connection.prepareStatement(
                            "CREATE DATABASE IF NOT EXISTS order_system");
            dbStmt.execute();
            dbStmt.close();
            PreparedStatement useStmt =
                    connection.prepareStatement(
                            "USE order_system");
            useStmt.execute();
            useStmt.close();
            PreparedStatement createOrderStmt =
                    connection.prepareStatement(
                            "CREATE TABLE IF NOT EXISTS order_table" +
                                    "(order_id bigint, order_name string," +
                                    "order_date string, order_amount int," +
                                    "order_valid boolean) ROW FORMAT DELIMITED " +
                                    "FIELDS TERMINATED BY '\\t' " +
                                    "LINES TERMINATED BY '\\n' STORED AS TEXTFILE");
            createOrderStmt.execute();
            createOrderStmt.close();
            releaseConnection(connection);
        } catch (SQLException e) {
            Log.e(TAG, "create table failed.", e);
        }
    }
    private static volatile OrderHiveDatabase DATABASE;

    public static OrderHiveDatabase getDatabase() {
        if (DATABASE == null) {
            synchronized (OrderHiveDatabase.class) {
                if (DATABASE == null) {
                    DATABASE = Light.databaseBuilder(OrderHiveDatabase.class,
                                    MysqlDialectProvider.class)
                            .setConnectionPool(OrderConnectionPool.class)
                            .setLogger(JdkDefaultLogger.getLogger("Order"))
                            .build();
                    DATABASE.init();
                }
            }
        }
        return DATABASE;
    }
}
